package cn.iocoder.yudao.module.mqtt.controller.admin.commport;

import org.checkerframework.checker.units.qual.C;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.commport.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.commport.CommPortDO;
import cn.iocoder.yudao.module.mqtt.service.commport.CommPortService;

@Tag(name = "管理后台 - 通讯口配置")
@RestController
@RequestMapping("/mqtt/comm-port")
@Validated
public class CommPortController {

    @Resource
    private CommPortService commPortService;

    @PostMapping("/create")
    @Operation(summary = "创建通讯口配置")
//    @PreAuthorize("@ss.hasPermission('mqtt:comm-port:create')")
    public CommonResult<Long> createCommPort(@Valid @RequestBody CommPortSaveReqVO createReqVO) {
        return success(commPortService.createCommPort(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通讯口配置")
//    @PreAuthorize("@ss.hasPermission('mqtt:comm-port:update')")
    public CommonResult<Boolean> updateCommPort(@Valid @RequestBody CommPortSaveReqVO updateReqVO) {
        commPortService.updateCommPort(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通讯口配置")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('mqtt:comm-port:delete')")
    public CommonResult<Boolean> deleteCommPort(@RequestParam("id") Long id) {
        commPortService.deleteCommPort(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除通讯口配置")
//                @PreAuthorize("@ss.hasPermission('mqtt:comm-port:delete')")
    public CommonResult<Boolean> deleteCommPortList(@RequestParam("ids") List<Long> ids) {
        commPortService.deleteCommPortListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通讯口配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('mqtt:comm-port:query')")
    public CommonResult<CommPortRespVO> getCommPort(@RequestParam("id") Long id) {
        CommPortDO commPort = commPortService.getCommPort(id);
        return success(BeanUtils.toBean(commPort, CommPortRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得通讯口配置分页")
//    @PreAuthorize("@ss.hasPermission('mqtt:comm-port:query')")
    public CommonResult<PageResult<CommPortRespVO>> getCommPortPage(@Valid CommPortPageReqVO pageReqVO) {
        PageResult<CommPortDO> pageResult = commPortService.getCommPortPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CommPortRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获取精简数据")
//    @PreAuthorize("@ss.hasPermission('mqtt:comm-port:query')")
    public CommonResult<List<CommPortRespVO>> getCommPortSimple() {
        List<CommPortDO> pageResult = commPortService.getSimple();
        return success( BeanUtils.toBean(pageResult, CommPortRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通讯口配置 Excel")
//    @PreAuthorize("@ss.hasPermission('mqtt:comm-port:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCommPortExcel(@Valid CommPortPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommPortDO> list = commPortService.getCommPortPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "通讯口配置.xls", "数据", CommPortRespVO.class,
                        BeanUtils.toBean(list, CommPortRespVO.class));
    }

}